package com.unionftech.common.utils

import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import com.unionftech.common.common.language.DynamicLanguageLoader
import java.lang.ref.WeakReference


object ProtocolUtil {
    private const val TR_1_START = "<tr_1>"

    private const val TR_1_END = "</tr_1>"

    private const val TR_2_START = "<tr_2>"

    private const val TR_2_END = "</tr_2>"

    const val CLIENT_POLICY_URL =
        "https://assets.cdn.neex.com/static/policy/%s/NeexLimitedClientAgreement.pdf"
    const val PRIVACY_POLICY_URL =
        "https://assets.cdn.neex.com/static/policy/%s/NeexLimitedPrivacyPolicy.pdf"
    const val TRADE_PRIVACY_POLICY_URL =
        "https://assets.cdn.neex.com/static/policy/%s/NEEXTermsOfUse.pdf"

    const val GOOGLE_CLIENT_POLICY_URL = "https://policies.google.com/privacy"
    const val GOOGLE_PRIVACY_POLICY_URL = "https://policies.google.com/terms"

    @JvmStatic
    fun setAgreementHas2Tr(
        textView: TextView,
        originText: String,
        privacyColor: Int,
        clientPolicyUrl: String,
        privacyPolicyUrl: String,
        isPdf: Boolean = true
    ) {
        val weakReference = WeakReference(textView.context.applicationContext)
        val context = weakReference.get() ?: return
        try {
            val strClientPolicy = originText.substring(
                originText.indexOf(TR_1_START) + TR_1_START.length, originText.indexOf(TR_1_END)
            )
            val strPrivacyPolicy = originText.substring(
                originText.indexOf(TR_2_START) + TR_2_START.length, originText.indexOf(TR_2_END)
            )
            val resultText =
                originText.replace(TR_1_START, "").replace(TR_1_END, "").replace(TR_2_START, "")
                    .replace(TR_2_END, "")
            val spannableString = SpannableString(resultText)
            var startIndex = resultText.indexOf(strClientPolicy)
            var endIndex = startIndex + strClientPolicy.length
            spannableString.setSpan(
                ForegroundColorSpan(context.getColor(privacyColor)),
                startIndex,
                endIndex,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            spannableString.setSpan(
                CustomClickableSpan(
                    strClientPolicy,
                    String.format(clientPolicyUrl, DynamicLanguageLoader.getCurLanguageCode()),
                    isUnderlineText = true,
                    isPdf = isPdf
                ),
                startIndex,
                endIndex,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )

            startIndex = resultText.indexOf(strPrivacyPolicy)
            endIndex = startIndex + strPrivacyPolicy.length
            spannableString.setSpan(
                ForegroundColorSpan(context.getColor(privacyColor)),
                startIndex,
                endIndex,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            spannableString.setSpan(
                CustomClickableSpan(
                    strPrivacyPolicy,
                    String.format(privacyPolicyUrl, DynamicLanguageLoader.getCurLanguageCode()),
                    isUnderlineText = true,
                    isPdf = isPdf
                ),
                startIndex,
                endIndex,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            textView.text = spannableString
            textView.movementMethod = LinkMovementMethod.getInstance()
            textView.highlightColor = context.getColor(android.R.color.transparent)
        } catch (e: Exception) {
            e.printStackTrace()
            weakReference.clear()
        }
    }

    @JvmStatic
    fun setAgreementHasTr(
        textView: TextView,
        originText: String,
        color: Int,
        linkUrl: String,
        isUnderlineText: Boolean,
        isPdf: Boolean = true
    ) {
        val weakReference = WeakReference(textView.context.applicationContext)
        val context = weakReference.get() ?: return
        try {
            val strClientPolicy = originText.substring(
                originText.indexOf(TR_1_START) + TR_1_START.length, originText.indexOf(TR_1_END)
            )
            val resultText =
                originText.replace(TR_1_START, "").replace(TR_1_END, "")
            val spannableString = SpannableString(resultText)
            val startIndex = resultText.indexOf(strClientPolicy)
            val endIndex = startIndex + strClientPolicy.length
            spannableString.setSpan(
                ForegroundColorSpan(context.getColor(color)),
                startIndex,
                endIndex,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            spannableString.setSpan(
                CustomClickableSpan(
                    strClientPolicy,
                    String.format(linkUrl, DynamicLanguageLoader.getCurLanguageCode()),
                    isUnderlineText = isUnderlineText,
                    isPdf = isPdf
                ),
                startIndex,
                endIndex,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            textView.text = spannableString
            textView.movementMethod = LinkMovementMethod.getInstance()
            textView.highlightColor = context.getColor(android.R.color.transparent)
        } catch (e: Exception) {
            e.printStackTrace()
            weakReference.clear()
        }
    }
}